var hash={};//类别id
var hashname={};//类别名
var hashparentid={};//所属类别
var idgetname={};//新闻id获取新闻名
var idgettype={};//新闻id获取类别
//加载全部类别列表
$.ajax({
    type:"get",
    //url:zonghenewsurl+190+"&userId="+id,
    url:"https://www.pulu.com/webapi/api/typeinfo/GetAllTypeInfo",
    datatype:"json",
    success:function(d){

        for(var r=0;r< d.Data.length;r++){
            var pinyinID=pinyin.getFullChars(d.Data[r].typeName).toLowerCase();
            if(!hash[pinyinID]){hash[pinyinID]=d.Data[r].typeId};
            if(!hashname[pinyinID]){hashname[pinyinID]=d.Data[r].typeName};
            if(!hashparentid[pinyinID]){hashparentid[pinyinID]=d.Data[r].parentId};
            if(!idgetname[d.Data[r].typeId]){idgetname[d.Data[r].typeId]=d.Data[r].typeName};
            if(!idgettype[d.Data[r].typeId]){idgettype[d.Data[r].typeId]=d.Data[r].parentId};
            //类别
            var listboxname="zhlistbox";
            if(d.Data[r].parentId==190){
                listboxname="zhlistbox";//综合
            }else if(d.Data[r].parentId==191){
                listboxname="hylistbox";//行业
            }else if(d.Data[r].parentId==192){
                listboxname="xqlistbox";//兴趣
            }
            $("#alwayspage ."+listboxname).append("<div class='listbtn choice' id='b"+ d.Data[r].typeId+"' onclick='alchoicethis(this)'>"+d.Data[r].typeName+"<div class='choice leftpart' id='"+pinyinID+"'></div><div class='choice rightpart'></div><img src='img/jiahao02.png' class='clickimg'/></div>")
            $("#professionList .zhlistbox").append('<div><label class="label'+d.Data[r].typeId+'" for="p'+d.Data[r].typeId+'">'+d.Data[r].typeName+'</label><input class="lf" id="p'+d.Data[r].typeId+'" type="checkbox" onchange="computeProfessiion(this)"/></div>')
        }
    }
});
//切换类别
$("nav a").click(function(e){
    $(window).scrollTop(0);
    $("nav a").attr("class","");
    $("#"+e.target.id).attr("class","active");
    $("#alwayspage .listplace").css("display","none");
    $("#alwayspage ."+e.target.id).css("display","block");
});
//选择三条新闻类别
$(".choicenews").click(function(){
    $("#choiceThree").css("display","block");
    $("#register").css("display","none");
});
//选择常看新闻-----------------------------------------------------------
var alchoicenum=0;//记录选择了几个行业
function alchoicethis(e){
    var thisid= e.id;
    var num=thisid.match(/[0-9]+/)[0];
    if(e.className=="listbtn choice"){
        alchoicenum++;
        e.className+=" isclicked";
        e.getElementsByTagName("img")[0].src="img/gou03.png";
        $("#alsearchinput").val("");
        //搜索的列表点击
        if(/s/.test(thisid)){
            $("#b"+num)[0].className+=" isclicked";
            $("#b"+num)[0].getElementsByTagName("img")[0].src="img/gou03.png";
            $(".searchlistplace div").remove();
            //$(".listplace").css("display","block"); //搜索展示注销
            $(".searchlistplace").css("display","none");
        }
        $("#register .newsname").append("<span id='c"+num+"' data-type='"+idgettype[num]+"'>"+idgetname[num]+"</span>");
        $("#choiceThree .choicenewsname").prepend(" <span id='d"+num+"' data-type='"+idgettype[num]+"'>"+idgetname[num]+"</span> ");
        $("#allchoicenum").html(alchoicenum);
        $("#allchoicenum2").html(alchoicenum);
    }
    else if(e.className=="listbtn choice isclicked"){
        alchoicenum--;
        e.className="listbtn choice";
        e.getElementsByTagName("img")[0].src="img/jiahao02.png";
        $("#alsearchinput").val("");
        if(/s/.test(thisid)){
            var num=thisid.match(/[0-9]+/)[0];
            $("#b"+num)[0].className="listbtn choice";
            $("#b"+num)[0].getElementsByTagName("img")[0].src="img/jiahao02.png";
            $(".searchlistplace div").remove();
            //$(".listplace").css("display","block");//搜索展示注销
            $(".searchlistplace").css("display","none");
        }
        $(".newsname span#c"+num).remove();
        $("#choiceThree span#d"+num).remove();
        $("#allchoicenum").html(alchoicenum);
        $("#allchoicenum2").html(alchoicenum);
    }
    else{
        $("#maxNine").css("display","block");
        setTimeout(function(){
            $("#maxNine").css("display","none");
            clearTimeout();
        },1500);
    }
    if(alchoicenum>0){
        //algobackbtn.style.display="block";
        $("#nextstep").attr("class","btn-primary full-width");
        $(".origword").css("display","none");
        $(".choicenewsname").css("display","-webkit-box");
    }else{
        //algobackbtn.style.display="none";
        $("#nextstep").attr("class","btn-primary full-width cannot");
        $(".origword").css("display","inline-block");
        $(".choicenewsname").css("display","none");
    }
    //$(".listplace").css("display","block");
    $(".searchlistplace").css("display","none");
}

var algobackbtn=document.getElementById("alwaysgobackbtn");//下一步
//algobackbtn.style.display="none";//页面初始状态 按钮隐藏
algobackbtn.onclick=function(){
    if(alchoicenum>0){
        $("#choiceThree").css("display","none");
        $("#register").css("display","block");
        $("#tishiinfor").html("");
    }else{
        $("#subscribeNote").css("display","block");
        setTimeout(function(){
            $("#subscribeNote").css("display","none");
        },1500)
    }

};
//搜索常看新闻
$("#alsearchbtn").click(function(){
    var word=$("#alsearchinput").val();
    $(".searchlistplace div").remove();
    if(!word.match(/(\s)/)&&word.length>0){
        var preg=new RegExp(word);
        $(".listplace").css("display","none");
        $(".searchlistplace").css("display","block");
        //var tomaodian=pinyin.getFullChars(word).toLowerCase();
        $.each(idgetname,function(i,da){
            if(preg.test(da)){
                var tomaodian=pinyin.getFullChars(da).toLowerCase();
                if(hash[tomaodian]){
                    if(hash[tomaodian]){
                        var clicked="";
                        imgurl="img/jiahao02.png";
                        if(/isclicked/.test($("#b"+hash[tomaodian]).attr("class"))){
                            clicked=" isclicked";
                            imgurl="img/gou03.png";
                        }
                        $("#alwayspage .searchlistplace").append("<div class='listbtn choice"+clicked+"' id='s"+hash[tomaodian]+"' onclick='alchoicethis(this)'>"+hashname[tomaodian]+"<div class='choice leftpart'></div><div class='choice rightpart'></div><img src='"+imgurl+"' class='clickimg'/></div>");
                    }
                }
            }
        })
    }
});
//返回按钮操作
$("#alwaysgoback").click(function(){
    $("#choiceThree").css("display","none");
    $("#register").css("display","block");
});
//选择行业-----------------------------------------------------------

//选择兴趣-----------------------------------------------------------



//键盘搜索--------------------------------------
document.onkeydown=function(event){
    var e=event||window.event||arguments.callee.caller.arguments[0];
    if(e&&e.keyCode==13){
        //搜索新闻
        if($("#alwayspage").css("display")==="block"){
            var word=$("#alsearchinput").val();
            $(".searchlistplace div").remove();
            if(!word.match(/(\s)/)&&word.length>0){
                var preg=new RegExp(word);
                $(".listplace").css("display","none");
                $(".searchlistplace").css("display","block");
                //var tomaodian=pinyin.getFullChars(word).toLowerCase();
                $.each(idgetname,function(i,da){
                    if(preg.test(da)){
                        var tomaodian=pinyin.getFullChars(da).toLowerCase();
                        if(hash[tomaodian]){
                            if(hash[tomaodian]){
                                var clicked="";
                                imgurl="img/jiahao02.png";
                                if(/isclicked/.test($("#b"+hash[tomaodian]).attr("class"))){
                                    clicked=" isclicked";
                                    imgurl="img/gou03.png";
                                }
                                $("#alwayspage .searchlistplace").append("<div class='listbtn choice"+clicked+"' id='s"+hash[tomaodian]+"' onclick='alchoicethis(this)'>"+hashname[tomaodian]+"<div class='choice leftpart'></div><div class='choice rightpart'></div><img src='"+imgurl+"' class='clickimg'/></div>");
                            }
                        }
                    }
                })

            }
        }
    }
};